//
//  WeatherSearchViewController.m
//  B
//
//  Created by 王展 on 15/11/8.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "WeatherSearchViewController.h"
#import "BaseView.h"
#import "ChineseToPinyin.h"

@interface WeatherSearchViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UIImageView *backImageView;
@property(nonatomic,strong)UIImage *backImage;

@property(nonatomic,strong)UITableView *searchTV;
@property(nonatomic,strong)UITableViewCell *cell;
@property(nonatomic,strong)UIImageView *headerView;

@end

@implementation WeatherSearchViewController

- (void) viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    for (UIView *view in self.tabBarController.view.subviews) {
        if ([view isKindOfClass:[BaseView class]]) {
            view.hidden = YES;
        }
    }

    self.navigationController.navigationBar.translucent = YES;
    
    [[[self.navigationController.navigationBar subviews]objectAtIndex:0] setAlpha:0];
}

- (void) viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    for (UIView *view in self.tabBarController.view.subviews) {
        if ([view isKindOfClass:[BaseView class]]) {
            view.hidden = NO;
        }
    }
    self.navigationController.navigationBar.translucent = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.view.autoresizesSubviews = NO;
     self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    UIImage *leftImage = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    
    [self createWeatherOrdinaryBackView];
    [self createSearchTV];
    [self createSearchView];
}

-(void)leftButtonAction
{
    [self.navigationController popViewControllerAnimated:NO];
    [self.myAgent searchLeftButtonAction];
}


-(void)createSearchTV
{
    self.searchTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT) style:UITableViewStylePlain];
    self.searchTV.backgroundColor = [UIColor clearColor];
    self.searchTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.searchTV.contentInset = UIEdgeInsetsMake(300*XHIGHT, 0, 0, 0);
    
    self.searchTV.delegate = self;
    self.searchTV.dataSource = self;
    
    [self.backImageView addSubview:self.searchTV];
    
    UIImage *headerImage = [UIImage imageNamed:@"searchview"];
    self.headerView =  [[UIImageView alloc] initWithFrame:CGRectMake(0, -300.0*XHIGHT, 375*XWIDTH, 300 *XHIGHT)];
    self.headerView.image = headerImage;
    self.headerView.userInteractionEnabled = YES;
    [self.searchTV addSubview:self.headerView];


}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.cell == nil) {
        self.cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    self.cell.backgroundColor = [UIColor clearColor];
    self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
 
    return self.cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 500 * XHIGHT;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
    CGFloat y = scrollView.contentOffset.y ;//加上导航栏高度,第一次是-200
    
    if (y < -200*XHIGHT) {
        CGRect frame = self.headerView.frame;
        frame.origin.y = y;//imageView的frame是不断往上偏移
        frame.size.height =  -y;//tablview向下偏移了多少,高度就增加多少
        self.headerView.frame = frame;
    }
    
    CGFloat naviAlpha = (HIGHT*0.3 + y + 64)/(HIGHT*0.3);
    [[[self.navigationController.navigationBar subviews]objectAtIndex:0] setAlpha:naviAlpha];
}


//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (self.searchTV == scrollView) {
//        
//    CGFloat y = scrollView.contentOffset.y;
//    if (y < -200 * XHIGHT) {
//        
//        self.headerView.frame = CGRectMake((200 * XHIGHT + y) / 2, y, WIDTH - (200 * XHIGHT + y),  - y);
//    }
//            CGFloat naviAlpha = (HIGHT*0.3 + y + 64)/(HIGHT*0.3);
//            [[[self.navigationController.navigationBar subviews]objectAtIndex:0] setAlpha:naviAlpha];
//    }
//}

-(void)createSearchView
{
    NSArray *cityNameArray =  @[@"北京", @"上海", @"大连", @"长春",  @"合肥", @"太原", @"沈阳", @"郑州", @"天津", @"辽源", @"常州", @"宁波", @"济南", @"青岛", @"厦门", @"西安", @"昆明", @"杭州", @"石家庄", @"广州", @"南京", @"长沙", @"武汉", @"深圳", @"丹东", @"通州", @"松原"];
    int k = 0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 9; j++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(27*XWIDTH + 125*XWIDTH * i, 100*XHIGHT + 40*XHIGHT * j, 80*XWIDTH, 30*XHIGHT);
            
            [button setTitle:[cityNameArray objectAtIndex:k] forState:UIControlStateNormal];
            
            button.layer.borderWidth = 0.5;
            button.layer.cornerRadius = 15*XHIGHT;
            button.layer.borderColor = [UIColor lightGrayColor].CGColor;
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
            [button setBackgroundColor:[UIColor colorWithRed:160 / 255.0 green:238 / 255.0 blue:255 / 255.0 alpha:0.5]];
            
            [button addTarget:self action:@selector(citybuttonAction:) forControlEvents:UIControlEventTouchUpInside];
            
            k++;
            [self.searchTV addSubview:button];
        }
    }
}

-(void)citybuttonAction:(UIButton *)button;
{
    NSLog(@"%@",button.titleLabel.text);
    NSString *cityName = button.titleLabel.text;
    
    //将字符串全变成小写字母
    cityName = [ChineseToPinyin pinyinFromChiniseString:cityName];
    cityName = [cityName lowercaseString];
    
    [self.myAgent searchCityName:cityName];
    [self.navigationController popViewControllerAnimated:NO];
}

-(void)createWeatherOrdinaryBackView
{
    self.backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.backImage = [UIImage imageNamed:@"back"];
//    self.backImageView.image = self.backImage;
    self.backImageView.backgroundColor = [UIColor colorWithRed:85/255.0 green:140/255.0 blue:220/255.0 alpha:1];
    [self.view addSubview:self.backImageView];
    self.backImageView.userInteractionEnabled = YES;
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
